<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-alert type="success" title="无需输入班级编号,系统会自动为您生成班级编号" class="dialog-alert" />
			<el-form ref="roleDialogFormRef" :model="state.ruleForm" size="default" label-width="90px" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" :offset="1" class="mb20">
						<el-form-item label="班级编号" prop="classId">
							<el-input v-model="state.ruleForm.classId" placeholder="无需输入班级编号" clearable disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" :offset="1" class="mb20">
						<el-form-item label="班级名称" prop="className">
							<el-input v-model="state.ruleForm.className" placeholder="请输入班级名称" clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="classManagementDialog">
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue';
import { addLessonInfo, updateLessonInfo } from '/@/api/class';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const roleDialogFormRef = ref(null);
const state = reactive({
	ruleForm: {
		classId: '',
		className: '',
	},
	menuData: [] as TreeType[],
	menuProps: {
		children: 'children',
		label: 'label',
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 验证规则
const rules = reactive({
	className: [{ required: true, message: '班级名称不得为空!', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (type: string, row: classManagementType) => {
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改班级';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增班级';
		state.dialog.submitTxt = '新 增';
		state.ruleForm = {
			classId: '',
			className: '',
		};
	}
	state.dialog.type = type;
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if (state.dialog.type === 'add') {
		roleDialogFormRef.value.validate((isValid) => {
			if (isValid) {
				emit('refresh');
				closeDialog();
				const lesson_name = {
					lesson_name: state.ruleForm.className,
				};
				addLessonInfo(lesson_name)
					.then((res) => {
						if (res.code === '2000') {
							ElMessage.success('新增班级成功!');
						} else {
							ElMessage.error('新增班级失败!');
						}
					})
					.catch((err) => {
						ElMessage.error('新增班级失败!');
					});
			} else {
				ElMessage.error('请检查表单数据!');
			}
		});
	} else {
		roleDialogFormRef.value.validate((isValid) => {
			if (isValid) {
				const updateInfo = {
					classId: state.ruleForm.classId,
					className: {
						lesson_name: state.ruleForm.className,
					}
				}
				updateLessonInfo(updateInfo).then((res) => {
					if (res.code === '2000') {
						ElMessage.success('修改班级成功!');
						emit('refresh');
						closeDialog();
					} else {
						ElMessage.error('修改班级失败!');
					}
				});
			}
			else {
				ElMessage.error('请检查表单数据!');
			}
		});
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}

	.dialog-alert {
		width: 700px;
		margin-left: 15px;
		margin-bottom: 20px;
	}
}
</style>
